$(function() {
	var myPano, latlng, lat = $('#lat'), lng = $('#lng'), yaw = 180, pitch = -10, yaw_view = $('#yaw'), pitch_view = $('#pitch');
	$.platform.addEvent({
		func: 'google_maps_streetView',
		event: {
			init: function(options) {
				if ($.window.exists('google_street_view')) {
					lat.val(options.lat);
					lng.val(options.lng);
					latlng = new GLatLng(options.lat, options.lng);
					myPano.setLocationAndPOV(latlng);
				} else {
					$.platform.app({
						app: 'google',
						sub_app: 'maps',
						method: 'streetView',
						params: {
							lat: options.lat,
							lng: options.lng
						}
					});
				}
			}
		}
	});
	$.getScript('http://www.google.com/jsapi?key=ABQIAAAAxRW0bkda2rrzoACHUIF9NxStud-h1rCQ67pMFslJs6oGqC9crBTbrz8OOhPMHB29HdqOyzEpkbLOpw',
		function() {
			google.load('maps', 2, {'callback': mapsloaded});
			function mapsloaded() {
				latlng = new GLatLng($('#lat').val(), $('#lng').val());
				panoramaOptions = {
					latlng: latlng,
					pov: {
						yaw: yaw,
						pitch: pitch
					}
				};
				yaw_view.html(yaw);
				pitch_view.html(pitch);
				myPano = new GStreetviewPanorama($('#pano').get(0), panoramaOptions);
				GEvent.addListener(myPano, "error", handleNoFlash);
	    	}
			function handleNoFlash(errorCode) {
				if (errorCode == FLASH_UNAVAILABLE) {
					alert("Error: Flash doesn't appear to be supported by your browser");
					return;
				}
			}
	});
	$('#yaw_left').click(
		function() {
			yaw -= 10;
			yaw_view.html(yaw);
			myPano.setLocationAndPOV(latlng, {yaw: yaw, pitch: pitch});
		}
	);
	$('#yaw_right').click(
		function() {
			yaw += 10;
			yaw_view.html(yaw);
			myPano.setLocationAndPOV(latlng, {yaw: yaw, pitch: pitch});
		}
	);
	$('#yaw_turn180').click(
		function() {
			yaw = yaw < 180 ? yaw + 180 : yaw - 180;
			yaw_view.html(yaw);
			myPano.setLocationAndPOV(latlng, {yaw: yaw, pitch: pitch});
		}
	);
	$('#pitch_up').click(
		function() {
			pitch = pitch >= -80 ? pitch - 10 : pitch;
			pitch_view.html(pitch);
			myPano.setLocationAndPOV(latlng, {yaw: yaw, pitch: pitch});
		}
	);
	$('#pitch_down').click(
		function() {
			pitch = pitch <=80 ? pitch + 10 : pitch;
			pitch_view.html(pitch);
			myPano.setLocationAndPOV(latlng, {yaw: yaw, pitch: pitch});
		}
	);
});